package com.bit.demo.controller;

import com.bit.demo.model.Person;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/login")
    public String login(String name){
        return "name: " + name;
    }
    @RequestMapping("/r1")
    public String r1(Integer age) {
        return "age: " + age;
    }
    @RequestMapping("/r2")
    public String r2(String name, String password) {
        return "name: " + name + ", password: " + password;
    }
    @RequestMapping("/r3")
    public String r3(Person person) {
        return "person: " + person;
    }
    @RequestMapping("/r4")
    public String r4(String[] arr) {
        return Arrays.toString(arr);
    }
    @RequestMapping("/rr4")
    public String rr4(String s) {//s不是必传，可以为null
        return "s: "+s;
    }
    @RequestMapping("/rr5")
    public String rr5(@RequestParam(value = "s",required = false) String name) {
        return "name: " + name;
    }
    @RequestMapping("/r5")
    public String r5(@RequestParam List<String> list) {
        return Arrays.toString(list.toArray());
    }
    @RequestMapping("/r6")
    public String r6(@RequestBody Person person) {
        return "person: " + person;
    }
    @RequestMapping("/article/{articleId}")
    public String r7(@PathVariable("articleId") Integer articleId){
        return "r7 文章id" + articleId;
    }
    @RequestMapping("/article/{articleId}/{type}")
    public String r8(@PathVariable("articleId") Integer articleId, @PathVariable("type") String type){
        return "r8 文章id" + articleId + " 类型" + type;
    }
    @RequestMapping("/r9")
    public String r9(@RequestPart("file111")MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        File destFile = new File("D:\\wangw\\temp\\" + originalFilename);
        //上传文件
        file.transferTo(destFile);
        return "上传成功";
    }
    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if(cookies != null) {
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "=" + cookie.getValue());
            }
        }
        return "getCookie成功！";
    }
    @RequestMapping("/getCookie2")
    public String getCoolie2(@CookieValue("name") String name) {
        return "获取cookie成功 name=" + name;
    }
    @RequestMapping("/setSession")
    public String setSession(String name, HttpServletRequest request) {
        //根据SessionId来查找到session
        //getSession()的括号中默认为true，如果没有则创建一个新的session
        //为false的话，没有session也不会创建。
        HttpSession session = request.getSession();
        //可以直接在形参中写HttpSession session，就不用HttpServletRequest了，这样就只能为true

        session.setAttribute("name",name);
        session.setAttribute("age",20);
        return "设置session成功";
    }
    @RequestMapping("/setSession2")
    public String setSession2(String name, HttpSession session) {
        session.setAttribute("name",name);
        session.setAttribute("age",20);
        return "设置session成功";
    }
    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request) {
        //为false的话，没有session也不会创建
        HttpSession session = request.getSession(false);

        String name = (String) session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        return "获取session成功 name=" + name + " age=" + age;
    }
    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session) {

        String name = (String) session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        return "获取session成功 name=" + name + " age=" + age;
    }
    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("name") String name) {
        return "获取session成功 name=" + name;
    }
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        return "从Header中获取信息，userAgent: " + userAgent;
    }
    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent) {
        return "从Header中获取信息，userAgent: " + userAgent;
    }

}
